package chapter_03.list_stack_queue;

import java.util.EmptyStackException;

/**
 * 栈的数组实现
 */
public class MyArrayStack {
    private Object[] array;
    private int top;
    private final static int size = 10;

    public MyArrayStack() {
        array = new Object[size];
        top = -1;
    }

    //压栈
    public void push(Object element) {
        if (top == size - 1) {
            throw new StackOverflowError();
        } else {
            array[++top] = element;
        }
    }

    //弹栈
    public Object pop() {
        if (top == -1) {
            throw new EmptyStackException();
        }
        return array[top--];
    }

    //判断是否为空
    public boolean isEmpty() {
        return top == -1;
    }

    //返回栈顶元素
    public Object peek() {
        if (top == -1) {
            throw new EmptyStackException();
        }
        return array[top];
    }
}
